package com.example.service;

import com.example.dao.OrdersDao;
import com.example.entity.Orders;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrdersService {

    @Resource
    private OrdersDao ordersDao;

    public List<Orders> findAll() {
        return ordersDao.selectAll();
    }

    public PageInfo<Orders> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Orders> list = ordersDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Orders order) {
        if(order.getUserId() == null) {
            throw new CustomException("用户ID不能为空");
        }
        ordersDao.insertSelective(order);
    }

    public void update(Orders order) {
        ordersDao.updateByPrimaryKeySelective(order);
    }

    public void delete(Integer orderId) {
        ordersDao.deleteByPrimaryKey(orderId);
    }

    public Orders findById(Integer orderId) {
        return ordersDao.selectByPrimaryKey(orderId);
    }
}
